﻿///////////////////////////////////////////////////////////////
// RLY08 interfacing example program                         //
// Written Jan 2008 by C Clarke                              //
// PLEASE MAKE SURE YOU USE A MAX232 OR RS232/USB COVERTER,  //
// SERIAL COMMS MUST BE AT TTL LEVELS, DIRECT CONNECTION     //
// OF RS232 WILL DESTROY THE RLY08!                          //
// ////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;          // added to include serial ports

namespace RLY08
{
    public partial class RLY08 : Form
    {
        static SerialPort rly08_port;        
        byte comport_open = 0;
        byte md23_found = 0;
        byte rly08_address = 0xb0;
        byte rly08_ver = 255;
        byte relay_states;
        byte[] SerBuf = new byte[20];

        public RLY08()
        {
            InitializeComponent();
            rly08_port = new SerialPort();           
            
            foreach (string s in SerialPort.GetPortNames())        // Get a list of available serial port names.
            {
                ports_combobox.Items.Add(s);                      // places each "COMx" name into combobox
            }
            ports_combobox.Text = "Select COM Port";              // printed only at first execution in the combobox
        }

        private void ports_combobox_SelectedIndexChanged(object sender, EventArgs e)
        {
            rly08_port.Close();                            // close any existing handle
            rly08_port.PortName = ports_combobox.Text;    // retrieves "COMx" from selection in combo box
            rly08_port.Parity = 0;
            rly08_port.BaudRate = 9600;
            rly08_port.StopBits = StopBits.Two;
            rly08_port.DataBits = 8;
            rly08_port.ReadTimeout = 50;
            rly08_port.WriteTimeout = 50;
            rly08_port.Open();
            comport_open = 1;
        }

        private void i2c_transmit(byte write_bytes)
        {
            try
            {
                rly08_port.Write(SerBuf, 0, write_bytes);      // writes specified amount of SerBuf out on COM port
            }
            catch (Exception)
            {
            }
        }

        private void i2c_recieve(byte read_bytes)
        {
            byte x;

            for (x = 0; x < read_bytes; x++)       // this will call the read function for the passed number times, 
            {                                      // this way it ensures each byte has been correctly recieved while
                try                                // still using timeouts
                {
                    rly08_port.Read(SerBuf, x, 1);     // retrieves 1 byte at a time and places in SerBuf at position x
                }
                catch (Exception)                   // timeout or other error occured
                {
                    SerBuf[0] = 255;
                }
            }
        }

        private void setbuttons()
        {
            SerBuf[0] = rly08_address;
            SerBuf[1] = 91;             // get realy states command
            i2c_transmit(2);            // transmit 2 bytes (SerBuf[0] + SerBuf[1])
            i2c_recieve(1);             // recieve 1 byte containing relay states
            relay_states = SerBuf[0];
            if ((relay_states & 1) == 1) relay1_Button.Checked = true;  // look at bits within recieved byte and set
            else relay1_Button.Checked = false;                         // or clear radio buttons accordingly
            if ((relay_states & 2) == 2) relay2_Button.Checked = true;
            else relay2_Button.Checked = false;
            if ((relay_states & 4) == 4) relay3_Button.Checked = true;
            else relay3_Button.Checked = false;
            if ((relay_states & 8) == 8) relay4_Button.Checked = true;
            else relay4_Button.Checked = false;
            if ((relay_states & 16) == 16) relay5_Button.Checked = true;
            else relay5_Button.Checked = false;
            if ((relay_states & 32) == 32) relay6_Button.Checked = true;
            else relay6_Button.Checked = false;
            if ((relay_states & 64) == 64) relay7_Button.Checked = true;
            else relay7_Button.Checked = false;
            if ((relay_states & 128) == 128) relay8_Button.Checked = true;
            else relay8_Button.Checked = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (comport_open == 1)
            {
                if (rly08_ver == 255)                   // no rly08 found, search addresses for reponse
                {
                    rly08_address++;                    // move up one address 
                    if (rly08_address > 8) rly08_address = 1; // top address is 8
                    rly08_add_textBox.Text = string.Format("search @ {0}", rly08_address);
                    SerBuf[0] = rly08_address;
                    SerBuf[1] = 90;                     // ask for software version command
                    i2c_transmit(2);                    // transmit 2 bytes (SerBuf[0] + SerBuf[1])
                    i2c_recieve(1);                     // and recieve version or 255 (timeout)
                    rly08_ver = SerBuf[0];              // save the recieved version 
                    
                }
                else
                {
                    rly08_add_textBox.Text = string.Format("found @ {0}", rly08_address);
                    rly08_ver_textBox.Text = string.Format("{0}", rly08_ver);
                    setbuttons();
                }
            }
        }

        private void relay1_Button_Clicked(object sender, EventArgs e)
        {
            if (rly08_ver != 255)           // only performed when a RLY08 is connected
            {
                SerBuf[0] = rly08_address;  
                if ((relay_states & 1) == 1) SerBuf[1] = 111;  // if the relay is set button click is for clearing
                else SerBuf[1] = 101;                          // else a set is required
                i2c_transmit(2);                               // transmit 2 bytes (SerBuf[0] + SerBuf[1])
            }
        }

        private void relay2_Button_Clicked(object sender, EventArgs e)
        {
            if (rly08_ver != 255)
            {
                SerBuf[0] = rly08_address;
                if ((relay_states & 2) == 2) SerBuf[1] = 112;
                else SerBuf[1] = 102;
                i2c_transmit(2);
            }
        }

        private void relay3_Button_Clicked(object sender, EventArgs e)
        {
            if (rly08_ver != 255)
            {
                SerBuf[0] = rly08_address;
                if ((relay_states & 4) == 4) SerBuf[1] = 113;
                else SerBuf[1] = 103;
                i2c_transmit(2);
            }
        }

        private void relay4_Button_Clicked(object sender, EventArgs e)
        {
            if (rly08_ver != 255)
            {
                SerBuf[0] = rly08_address;
                if ((relay_states & 8) == 8) SerBuf[1] = 114;
                else SerBuf[1] = 104;
                i2c_transmit(2);
            }
        }

        private void relay5_Button_Clicked(object sender, EventArgs e)
        {
            if (rly08_ver != 255)
            {
                SerBuf[0] = rly08_address;
                if ((relay_states & 16) == 16) SerBuf[1] = 115;
                else SerBuf[1] = 105;
                i2c_transmit(2);
            }
        }

        private void relay6_Button_Clicked(object sender, EventArgs e)
        {
            if (rly08_ver != 255)
            {
                SerBuf[0] = rly08_address;
                if ((relay_states & 32) == 32) SerBuf[1] = 116;
                else SerBuf[1] = 106;
                i2c_transmit(2);
            }
        }

        private void relay7_Button_Clicked(object sender, EventArgs e)
        {
            if (rly08_ver != 255)
            {
                SerBuf[0] = rly08_address;
                if ((relay_states & 64) == 64) SerBuf[1] = 117;
                else SerBuf[1] = 107;
                i2c_transmit(2);
            }
        }

        private void relay8_Button_Clicked(object sender, EventArgs e)
        {
            if (rly08_ver != 255)
            {
                SerBuf[0] = rly08_address;
                if ((relay_states & 128) == 128) SerBuf[1] = 118;
                else SerBuf[1] = 108;
                i2c_transmit(2);
            }
        }

        private void All_on_button_Click(object sender, EventArgs e)
        {
            SerBuf[0] = rly08_address;
            SerBuf[1] = 100;            // all on command
            i2c_transmit(2);
        }

        private void All_off_button_Click(object sender, EventArgs e)
        {
            SerBuf[0] = rly08_address;
            SerBuf[1] = 110;            // all off command
            i2c_transmit(2);
        }

    }
}